package cn.hxzy.teacher.service.impl;


import cn.hxzy.common.domain.Direction;
import cn.hxzy.common.domain.SysSubject;
import cn.hxzy.common.domain.SysSubjectLabel;
import cn.hxzy.teacher.mapper.SysSubjectMapper;
import cn.hxzy.teacher.service.ISysSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tyhxzy
 * @since 2024-12-14
 */
@Service
public class SysSubjectServiceImpl implements ISysSubjectService {
    @Autowired
    private SysSubjectMapper sysSubjectMapper;

    @Override
    public Integer addSubject(SysSubject sysSubject) {
        return sysSubjectMapper.addSubject(sysSubject);
    }

    //查询标签
    @Override
    public List<SysSubjectLabel> selectSubjectLabel() {
        return sysSubjectMapper.selectSubjectLabel();
    }

    //查询专业名称
    @Override
    public List<Direction> selectDirection() {
        return sysSubjectMapper.selectDirection();
    }


    //修改题目
    @Override
    public Integer updateSysSubject(SysSubject sysSubject) {
        return sysSubjectMapper.updateSysSubject(sysSubject);
    }

    private List<SysSubject> processSubjects(List<SysSubject> subjects) {
        List<SysSubject> result = new ArrayList<>();
        for (SysSubject st : subjects) {
            SysSubject ss = new SysSubject();
            ss.setSubjectId(st.getSubjectId());
            ss.setSubjectName(st.getSubjectName());
            ss.setOptionA(st.getOptionA());
            ss.setOptionB(st.getOptionB());
            ss.setOptionC(st.getOptionC());
            ss.setOptionD(st.getOptionD());
            ss.setOptionE(st.getOptionE());
            ss.setOptionF(st.getOptionF());
            ss.setSubjectAnswer(st.getSubjectAnswer());
            ss.setSubjectType(st.getSubjectType());
            ss.setSubjectLabel(st.getSubjectLabel());
            ss.setSubjectMajor(st.getSubjectMajor());
            ss.setSubjectImage(st.getSubjectImage());
            ss.setSubjectLv(st.getSubjectLv());
            ss.setCreator(st.getCreator());
            ss.setCreatetime(st.getCreatetime());
            ss.setReviser(st.getReviser());
            ss.setChangetime(st.getChangetime());
            ss.setUser(st.getUser());
            ss.setDirection(st.getDirection());
            // 将 String 转换为 List<Integer>
            List<Integer> labelList = new ArrayList<>();
            if (st.getSubjectLabel() != null && !st.getSubjectLabel().isEmpty()) {
                String[] labelIds = st.getSubjectLabel().split(",");
                for (String labelId : labelIds) {
                    try {
                        int id = Integer.parseInt(labelId.trim());
                        labelList.add(id);
                    } catch (NumberFormatException e) {
                        // 处理转换异常，例如记录日志
                        System.err.println("Invalid label ID: " + labelId);
                    }
                }
            }

            // 打印 labelList 以验证内容
            System.out.println("labelList: " + labelList);

            // 假设 sysSubjectMapper.syssubjectlabel 返回的是 List<SysSubjectLabel>
            List<SysSubjectLabel> labels = sysSubjectMapper.syssubjectlabel(labelList);

            // 打印 labels 以验证内容
            System.out.println("labels: " + labels);

//           List<SysSubjectLabel> labelNames = new ArrayList<>();
            // 遍历 labels 并设置属性
            for (SysSubjectLabel label : labels) {
                System.out.println("Label ID: " + label.getLabelId());
                System.out.println("Label Name: " + label.getLabelName());
                System.out.println("Major ID: " + label.getMajorId());
            }
            ss.setSysSubjectLabel(labels);
            // 查询题目的所属专业
            ss.setDirection(sysSubjectMapper.sysdirection(st.getSubjectMajor()));
            // 查询题目的创建人
            ss.setUser(sysSubjectMapper.sysuser(st.getCreator()));
            result.add(ss);
        }
        return result;
    }

    @Override
    public List<SysSubject> selectquanbu(Integer start, Integer pageSize) {
        List<SysSubject> selectquanbu = sysSubjectMapper.selectquanbu(start, pageSize);
        List<SysSubject> selectquanbu1 = new ArrayList<>();
        System.out.println("------------------------------------");
        System.out.println(selectquanbu);
        selectquanbu1 = processSubjects(selectquanbu);
        System.out.println("+++++++++++++++++++++++");
        System.out.println(selectquanbu1);
        return selectquanbu1;
    }


    @Override
    public Integer selectzong() {
        Integer selectzong = sysSubjectMapper.selectzong();
        return selectzong;
    }

    @Override
    public List<SysSubject> selectType(Integer start, Integer pageSize, Integer subjectType) {
        List<SysSubject> selecttype = sysSubjectMapper.selecttype(start, pageSize, subjectType);
        List<SysSubject> type = new ArrayList<>();
        type = processSubjects(selecttype);
        return type;
    }

    @Override
    public Integer selecttixing(Integer subjectType) {
        Integer selecttixing = sysSubjectMapper.selecttixing(subjectType);
        return selecttixing;
    }


    @Override
    public Integer selectbiaoqianshu(List<Integer> shu, Integer slv, Integer tt, Integer zz) {
        System.out.println("slv:" + slv);
        Integer selectbiaoqianshu = sysSubjectMapper.selectbiaoqianshu(shu, slv, tt, zz);
        return selectbiaoqianshu;
    }

    @Override
    public List<SysSubject> selectbiaoqian(Integer start, Integer pageSize, List<Integer> shu, Integer slv, Integer tt, Integer zz) {
        System.out.println("shu:" + shu);
        System.out.println("start:" + start);
        System.out.println("pageSize:" + pageSize);
        System.out.println("slv:" + slv);
        List<SysSubject> selectbiaoqian = sysSubjectMapper.selectbiaoqian(start, pageSize, shu, slv, tt, zz);
        System.out.println("selectbiaoqian:" + selectbiaoqian);
        List<SysSubject> xx = new ArrayList<>();
        xx = processSubjects(selectbiaoqian);
        return xx;
    }

    @Override
    public List<SysSubject> selectxxx(Integer start, Integer pageSize, Integer slv, Integer tt, Integer zz) {
        List<SysSubject> selectxxx = sysSubjectMapper.selectxxx(start, pageSize, slv, tt, zz);
        List<SysSubject> xx = new ArrayList<>();
        xx = processSubjects(selectxxx);
        return xx;
    }

    @Override
    public Integer selectxxxzong(Integer slv, Integer tt, Integer zz) {
        Integer selectxxxzong = sysSubjectMapper.selectxxxzong(slv, tt, zz);
        return selectxxxzong;
    }

    @Override
    public List<Direction> major() {
        List<Direction> selectzhuanye = sysSubjectMapper.selectzhuanye();
        return selectzhuanye;
    }

    //逻辑删除题目
    @Override
    public Integer deleteById(Long subjectId) {
        return sysSubjectMapper.deleteById(subjectId);
    }


}
